package com._520it.crm.service.impl;

import com._520it.crm.domain.Client;
import com._520it.crm.domain.Employee;
import com._520it.crm.mapper.ClientMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.QueryObject;
import com._520it.crm.service.IClientService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/10/9.
 */
@Service
public class ClientServiceImpl implements IClientService {
    @Autowired
    private ClientMapper clientMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return clientMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Client record) {
        record.setPassword("1");//默认密码为1
        record.setStatus(Client.ZERO);//潜在客户
        record.setInputtime(new Date());//录入时间
        Employee current = (Employee) SecurityUtils.getSubject().getPrincipal();//获取当前登录用户
        record.setInputor(current);
        record.setManager(current);
        record.setSharer(null);
        record.setClientsn(new Date().getTime());//客户编号
        return clientMapper.insert(record);
    }

    @Override
    public Client selectByPrimaryKey(Long id) {
        return clientMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Client> selectAll() {
        return clientMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Client record) {
        return clientMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult queryPage(QueryObject qo) {
        Long count = clientMapper.queryPageCount(qo);
        if (count <= 0) {
            return new PageResult(0L, Collections.EMPTY_LIST);
        }
        List<Client> result = clientMapper.queryPageDataResult(qo);
        PageResult pageResult = new PageResult(count, result);
        return pageResult;
    }

    @Override
    public void invalidByPrimaryKey(Long clientId) {
        clientMapper.invalidByPrimaryKey(clientId);
    }

    @Override
    public void formalByPrimaryKey(Long clientId) {
        clientMapper.formalByPrimaryKey(clientId);
    }

    @Override
    public void putPoolByPrimaryKey(Long clientId) {
        Client client = clientMapper.selectByPrimaryKey(clientId);
        Employee manager = client.getManager();
        if (manager == null) {
            return;
        }
        clientMapper.updateMS(clientId, manager.getId());
    }

    @Override
    public void acquire(Long clientId) {
        Employee current = (Employee) SecurityUtils.getSubject().getPrincipal();
        clientMapper.updateManager(clientId, current.getId());
    }

    @Override
    public void updateManager(Long clientId, Long managerId) {
        clientMapper.updateManager(clientId, managerId);
    }

    @Override
    public List<Client> selectPotentialClient() {
        return clientMapper.selectPotentialClient();
    }

    @Override
    public Client selectByOrderBillId(Long orderBillId) {
        return clientMapper.selectByOrderBillId(orderBillId);
    }

    @Override
    public void updateFromRivise(Client client) {
        clientMapper.updateFromRivise(client);
    }

    @Override
    public Client queryByidcardnumber(String idcardnumber) {
        return clientMapper.queryByidcardnumber(idcardnumber);
    }

    @Override
    public int insertByExcelUpload(Client client) {
        client.setPassword("1");//默认密码为1
        return clientMapper.insertByExcelUpload(client);
    }

    @Override
    public void updateClient(Client client) {
        clientMapper.updateClient(client);
    }
}
